/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import diagram.Graphical;
import diagram.connections.TriggerConnection;
import diagram.handles.TriggerHandle;
import diagram.handles.ResizeHandle;
import model.DiagramModel;
import java.awt.event.ActionEvent;
import java.util.ConcurrentModificationException;
import java.util.Set;
import javax.swing.AbstractAction;

/**
 *
 * @author Jelle
 */
public class DeleteAction extends AbstractAction {

    private DiagramModel model;
    private int modus;

    public DeleteAction(String mode, DiagramModel model) {
        this.model = model;
        if (mode.equals("all")) {
            if (model.getSelected().getClass().getName().equals("diagram.Node")) {
                modus = 0;
            } else {
                modus = 2;
            }
        } else if (mode.equals("normal")) {
            modus = 1;
        } else if (mode.equals("connections")) {
            modus = 3;

        }
    }

    public void actionPerformed(ActionEvent e) {
        ResizeHandle r = null;
        Set<Graphical> ck = null;
        switch (modus) {
            case 0:
                System.out.println("DeleteAction modus 0 (all = node + all handles)");
                r = (ResizeHandle) model.getAttachedObject(model.getSelected(), "resizehandle");
                ck = model.getAttachedObjects(model.getSelected(), "all");
                if (ck != null) {
                    try {
                        for (Graphical g : model.getObjects("triggerconnection")) {
                            TriggerConnection c = (TriggerConnection) g;
                            if (ck.equals(c.getHandleSource()) || ck.equals(c.getHandleTarget())) {
                                model.removeObject(c);
                            }
                        }
                        for (Graphical h : ck) {
                            model.removeObject(h);
                        }
                    } catch (ConcurrentModificationException ex) {
                    }
                }
                model.removeObject(r);
                model.removeObject(model.getSelected());
            case 1:
                System.out.println("DeleteAction modus 1 (normal = node + resizehandle)");
                if (model.getSelection("all").size() != 0) {
                    r = (ResizeHandle) model.getAttachedObject(model.getSelected(), "resizehandle");
                    if (r != null) {
                        model.removeObject(r);
                    }
                    model.removeObject(model.getSelected());
                }

            case 2:
                if (model.getSelection("all").size() != 0) {
                    System.out.println("DeleteAction modus 2 (triggerHandle)");

                    Graphical gr = model.getSelected();
                    Class toBedefined; // java reflection
                    ClassLoader classLoader = this.getClass().getClassLoader();
                    try {
                        toBedefined = classLoader.loadClass(gr.getClass().getName());
                        System.out.println("c:" + toBedefined.getName());

                        if (toBedefined.getName().equals("diagram.handles.TriggerHandle")) {
                            TriggerHandle cc = (TriggerHandle) model.getSelected();
                            System.out.println("conns" + model.getObjects("connection").size());
                            try {
                                for (Graphical g : model.getObjects("triggerconnection")) {
                                    TriggerConnection c = (TriggerConnection) g;
                                    if (cc.equals(c.getHandleSource()) || cc.equals(c.getHandleTarget())) {
                                        System.out.println("delete trigger found conn");
                                        model.removeObject(c);
                                    }
                                }
                            } catch (ConcurrentModificationException ex) {
                            }
                        }
                    } catch (Exception ex) {
                    }
                    model.removeObject(model.getSelected());
                }
            case 3:
//                System.out.println("DeleteAction modus 3 (connections)");
//                ArrayList<Graphical> cs = model.getSelection("handles");
//                for (Connection c : model.getConnections()) {
//                    for (Graphical g : cs) {
//                        Connection bb = (Connection)g;
//                        if (bb.equals(c) || bb.equals(c)) {
//                            model.removeConnection(c);
//                        }
//                    }
//                }
        }
    }
}
